package services

import beans.Record
import base.Message
import base.State
import controllers.RecordController
import models.BoughtM
import models.CollectionM
import models.RecordM

class RecordService(controller: RecordController):
        BaseService<RecordController>(controller) {

    fun getPublicRecordByUserId(userId: Int){
        val list = RecordM.findRecordByUserId(userId)
        val message = Message(State.STATE_SUCCESS, data = list)
        controller.renderJson(message)
    }

    fun getBoughtRecordByUserId(userId: Int){
        val list = BoughtM.findBoughtByUserId(userId)
        if (list.isEmpty()){
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_RECORD_NOT_ANY_BOUGHT))
            controller.renderJson(message)
            return
        }
        val records = list.map { model ->
            val recordId = model.getInt(BoughtM.RECORD_ID)
            val recordM = RecordM.AGENT.findById(recordId)
            return@map Record.from(recordM)
        }
        val message = Message(State.STATE_SUCCESS, data = records)
        controller.renderJson(message)
    }

    fun getCollectionByUserId(userId: Int) {
        val list = CollectionM.findByUserId(userId)
        if (list.isEmpty()){
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_RECORD_NOT_ANY_COLLECTION))
            controller.renderJson(message)
            return
        }
        val records = list.map { model ->
            val recordId = model.getInt(BoughtM.RECORD_ID)
            val recordM =    RecordM.AGENT.findById(recordId)
            return@map Record.from(recordM)
        }
        val message = Message(State.STATE_SUCCESS, data = records)
        controller.renderJson(message)
    }

    fun getSoldRecordByUserId(userId: Int) {
       val list = RecordM.findSoldRecords(userId)
        if (list.isEmpty()){
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_RECORD_NOT_ANY_SOLD))
            controller.renderJson(message)
            return
        }
        val message = Message(State.STATE_SUCCESS, data = list)
        controller.renderJson(message)
    }

    fun checkCollect(userId: Int, recordId: Int){
        val model = CollectionM.checkCollect(userId, recordId)
        val code = if (model == null) State.STATE_FAIL else State.STATE_SUCCESS
        val message = Message(code, data = "")
        controller.renderJson(message)
    }

    fun collectOrCancel(userId: Int, recordId: Int, flag: Boolean){
        val model = CollectionM.checkCollect(userId, recordId)
        if (flag){
            //收藏
            if (model != null){
                val message = Message(State.STATE_SUCCESS, data = "")
                controller.renderJson(message)
                return
            }
            val collectionM = CollectionM()
            collectionM[CollectionM.USER_ID] = userId
            collectionM[CollectionM.RECORD_ID] = recordId
            if (collectionM.save()){
                val message = Message(State.STATE_SUCCESS, data = "")
                controller.renderJson(message)
            } else {
                val message = Message(State.STATE_FAIL, data = "")
                controller.renderJson(message)
            }
        } else {
            //取消
            if (model == null){
                val message = Message(State.STATE_SUCCESS, data = "")
                controller.renderJson(message)
                return
            }
            val code = if (model.delete()) State.STATE_SUCCESS else State.STATE_FAIL
            val message = Message(code, data = "")
            controller.renderJson(message)
        }
    }

}

object RecordState{
    const val REVIEW = 0
    const val PASS = 1
    const val UNPASS = 2
    const val SOLD = 3
}